//
//  MYGPatientImageLayoutAlert.swift
//  MYDoctor
//
//  Created by Denny on 2024/5/6.
//

import UIKit

class MYGPatientImageLayoutAlert: UIView {
    var dataSource = [ImagesModel]()
    var tapLayoutIndex:((Int)->Void)?
    var title:UILabel = {
        let control = UILabel()
        control.text = "视图"
        control.font = .medium(16)
        control.textColor = UIColor.init(hexString: "#232323")
        return control
    }()
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 12
        layout.minimumInteritemSpacing = 8
        var leftMargin = 33.0
        var midMargin = 8.0
        let itemWidth = (screenWidth - 2 * leftMargin - midMargin) / 2
        layout.itemSize = CGSize(width: itemWidth, height: 41)
        let control = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        control.register(UINib.init(nibName: "MYGImagesLayoutCell", bundle: nil), forCellWithReuseIdentifier: "MYGImagesLayoutCell")
        control.showsHorizontalScrollIndicator = false
        control.showsVerticalScrollIndicator = false
        control.backgroundColor = .white
        control.delegate = self
        control.dataSource = self
        control.contentInset = UIEdgeInsets(top: 0, left: leftMargin, bottom: 0, right: leftMargin)
        return control
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(title)
        title.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(33)
            make.top.equalToSuperview().offset(20)
        }
        
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(title.snp.bottom).offset(13)
            make.left.bottom.right.equalToSuperview()
        }
        configData()
    }
    
    func didSelectIndex(index: Int) {
        self.collectionView.delegate?.collectionView?(self.collectionView, didSelectItemAt: IndexPath(row: index, section: 0))
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configData(){
        let layout1 = configModel(title: "标准视图", iconName: "Myg_image_layout1_selected")
        let layout2 = configModel(title: "缩略视图", iconName: "Myg_image_layout2_selected")
        let layout3 = configModel(title: "列表视图", iconName: "Myg_image_layout3_selected")
        self.dataSource.append(layout1)
        self.dataSource.append(layout2)
        self.dataSource.append(layout3)
        self.collectionView.reloadData()
    }
    func configModel(title:String,iconName:String) ->ImagesModel{
        var model = ImagesModel()
        model.title = title
        model.iconString = iconName
        return model
    }
    
}

extension MYGPatientImageLayoutAlert: UICollectionViewDataSource,UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGImagesLayoutCell", for: indexPath) as! MYGImagesLayoutCell
        let model = self.dataSource[indexPath.row]
        cell.model = model
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.dataSource[indexPath.row]
        self.dataSource = self.dataSource.map({ obj in
            var result = obj
            result.selected = (obj.title == model.title)
            return result
        })
        self.collectionView.reloadData()
        if let c = tapLayoutIndex {
            c(indexPath.row)
        }
    }
   
}


struct ImagesModel {
    var iconString = ""
    var selected = false
    var title = ""
}

